// pages/new_group/new_group.js
Page({

  /**
   * Page initial data
   */
  data: {
    value: "",
    btn: 0
  },

  onChange(e) {
    console.log('onChange', e)
    this.setData({
      value: e.detail.value,
    })
  },

  handleClick(e) {
    var that = this
    this.setData({
      btn: 1
    })

    wx.showLoading({
      title: '创建中',
    })

    wx.login({
      success(resout) {
        if (resout.code) {
          // 发起网络请求
          wx.request({
            url: 'https://www.ourspark.org/helloworld/addGroup',
            data: {
              code: resout.code,
              name: that.data.value
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(res) {
              console.log(res)
              if (res.data.error_no == 2) {
                wx.hideLoading()
                that.setData({
                  btn: 0
                })
                wx.showModal({
                  title: "哎呀~",
                  content: res.data.error_msg,
                })
              } else if (res.data.error_no == 1) {
                wx.hideLoading()
                that.setData({
                  value: "",
                  btn: 0
                })

                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 1000
                });
              }
            },
            fail: function(res) {
              wx.hideLoading()
              that.setData({
                value: "",
                btn: 0
              })

              wx.showToast({
                title: '失败',
                icon: 'none',
                duration: 1000
              });
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad: function(options) {


  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady: function() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow: function() {

  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide: function() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload: function() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh: function() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom: function() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage: function() {

  }
})